/****************************************************************************
	This file is part of Basqet.

	Basqet is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Basqet is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
    along with Basqet.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************************/


#include "basketcontent.h"

#include "notedatafactory.h"
#include "basketdescriptor.h"

#include "notepadpagedata.h"

#include "notedata.h"
#include "textnotedata.h"

#include <QTextDocument>
#include <QTextDocumentWriter>
#include <QTextBlock>
#include <QTextCursor>
#include <QTextDocumentFragment>

#include <QDomNodeList>
#include <QtDebug>

const QString DESCRIPTOR_FILE_NAME("basket.xml");

BasketContent::BasketContent(const QString& location)
		:mBasketDescriptor(new BasketDescriptor(location, DESCRIPTOR_FILE_NAME))
{
	mNotePadPageData = new NotePadPageData(mBasketDescriptor->getNotePadPageRoot());

}


QExplicitlySharedDataPointer<NotePadPageData>	BasketContent::getNotePadPageData() const
{
	return mNotePadPageData;

}

QList< QExplicitlySharedDataPointer<NoteData> >	BasketContent::getNotes() const
{
	return mNotes;
}

QExplicitlySharedDataPointer<NoteData> BasketContent::createTextNote(QPoint place)
{
	QExplicitlySharedDataPointer<NoteData> pNote = QExplicitlySharedDataPointer<NoteData>(NoteDataFactory::createNewTextNote(mBasketDescriptor.data(), this));

	if (!place.isNull())
	{
		pNote->setGeometry(place, QSize());
	}

	mNotes.append(pNote);

	emit noteDataCreated(pNote);

	return pNote;

}

QExplicitlySharedDataPointer<NoteData> BasketContent::createTextNote(const NoteData* copyFrom/* = 0*/)
{
	QExplicitlySharedDataPointer<NoteData> pNote = QExplicitlySharedDataPointer<NoteData>(NoteDataFactory::createNewTextNote(mBasketDescriptor.data(), this));

	if (copyFrom)
	{
		pNote->copyFrom(copyFrom);
	}

	mNotes.append(pNote);

	emit noteDataCreated(pNote);

	return pNote;

}



void BasketContent::deleteTextNote(QExplicitlySharedDataPointer<NoteData> pNote)
{
	pNote->removeNote();
	mNotes.removeAll(pNote);

}


void BasketContent::openBasket()
{
	QDomNodeList notes = mBasketDescriptor->getNoteObjects();

	int count = notes.count();

	for(int index = 0; index < count; index++)
	{
		QExplicitlySharedDataPointer<NoteData>	pNote(NoteDataFactory::createNoteByXML(mBasketDescriptor.data(), this, notes.at(index).toElement()));
		mNotes.append(pNote);

		emit noteDataCreated(pNote);
	}

}

void BasketContent::actionExportBasketTo(QString fileName)
{

	qDebug() << "BasketContent::Exporting document to " << fileName;

    qDebug() << "Export formats available" << QTextDocumentWriter::supportedDocumentFormats();

	QTextDocument	document;
	QTextCursor		cursor(&document);
    QTextDocumentWriter	writer(fileName);

	foreach(QExplicitlySharedDataPointer<NoteData> pNoteData, mNotes)
	{
		TextNoteData* pTextNote = dynamic_cast<TextNoteData*>(pNoteData.data());

		if (pTextNote)
		{

            if (!pTextNote->getHeaderDocument()->toPlainText().isEmpty())
            {
                cursor.insertHtml("<h5>Header:</h5><br>");
                cursor.insertFragment(QTextDocumentFragment(pTextNote->getHeaderDocument()));
                cursor.insertHtml("<br><h5>Body:</h5><br>");
            }
            cursor.insertFragment(QTextDocumentFragment(pTextNote->getDocument()));
            cursor.insertHtml("<br><hr/><br>");


//            writer.write(pTextNote->getHeaderDocument());
//            writer.write(pTextNote->getDocument());


//			cursor.insertHtml(pTextNote->getHeaderDocument()->toHtml());
//            qDebug() << "Adding body:" << pTextNote->getDocument()->toHtml();
//			cursor.insertHtml(pTextNote->getDocument()->toHtml());

		}

	}


    writer.write(&document);

}


void BasketContent::actionSave()
{
	mBasketDescriptor->save();

	foreach(QExplicitlySharedDataPointer<NoteData> pNoteData, mNotes)
	{
		pNoteData->save();
	}
}

void BasketContent::printXML()
{
	mBasketDescriptor->printXML();

}

